
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
namespace API接口示范
{
    public partial class Form1 : Form
    {
        private BackgroundWorker backgroundWorker;
       
        private WebHostService _webHost;
        public Form1()
        {
            InitializeComponent();
            InitializeWebApi();
          
        }
        private void InitializeWebApi()
        {
            _webHost = new WebHostService(8608);
            ControllerController.GetTextBoxValue1 = (a) => Querymethod(a);
            ControllerController.Control= (a) => Controlmethod0(a);
            _webHost.Start();
            
        }
        public Querydata Querymethod(int a)
        {
            if (a ==1 )
            {

                Querydata data1 = new Querydata { qdata = "设备日志", content = textBox1.Text, status = zt };
                return data1;
            }
            else if (a ==2)
            {

                Querydata data2 = new Querydata { qdata = "设备状态", content = label1.Text, status = zt };
                return data2;
            }
            else if (a == 3)
            {

                Querydata data3 = new Querydata { qdata = "灯", content = den, status = zt };
                return data3;
            }
            else
            {

                Querydata data4 = new Querydata { qdata = "没有数据", content = "数据不存在", status = zt };
                return data4;
            }

        }
        public bool Controlmethod0(string a)
        {
            if (textBox1.InvokeRequired)
            {              
                Func<bool> del = () => Controlmethod1(a);
                IAsyncResult asyncResult = textBox1.BeginInvoke(del);
                // 等待异步操作完成并获取结果
                bool result = (bool)textBox1.EndInvoke(asyncResult);
                return result;
            }
            else
            {
                return Controlmethod1(a);
            }
        }
        public bool Controlmethod1(string a)
        {
            if (a == "急停")
            {

                button1_Click(null, EventArgs.Empty);
                return true;
            }
            else if (a == "启动")
            {

                button2_Click(null, EventArgs.Empty);
                return true;
            }
            else if (a == "停止")
            {

                button3_Click(null, EventArgs.Empty);
                return true;
            }
            else if (a == "复位")
            {

                button4_Click(null, EventArgs.Empty);
                return true;
            }
            else
            {              
                return false;
            }

        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
           
            _webHost?.Stop();
            base.OnFormClosing(e);
            
        }
        int zt = 0;
        string den = "黄";
        
        private void button1_Click(object sender, EventArgs e)
        {

            label1.Text = "急停中";
            zt = -2;
            pictureBox1.BackColor = Color.FromArgb(255, 255, 128, 128);
            den = "红";
            button1.BackColor = Color.PaleTurquoise;
            button2.BackColor = SystemColors.ButtonHighlight;
            button3.BackColor = SystemColors.ButtonHighlight;
            button4.BackColor = SystemColors.ButtonHighlight;
            DateTime currentTime = DateTime.Now;
            string timeString = currentTime.ToString("HH:mm");
            textBox1.Text = timeString + "点击急停";

        }

        private void button2_Click(object sender, EventArgs e)
        {
           // Debug.WriteLine($"识别开始暂停A:{DateTime.Now:HH:mm:ss.fff}");
           //// Task.Delay(2000);
           // Thread.Sleep(2000);
           // Debug.WriteLine($"识别开始暂停B:{DateTime.Now:HH:mm:ss.fff}");
            if (zt == 0)
            {
                label1.Text = "运行中";
                zt = 1;
                pictureBox1.BackColor = Color.FromArgb(255, 128, 255, 128);
                den = "绿";
                button2.BackColor = Color.PaleTurquoise;
                button1.BackColor = SystemColors.ButtonHighlight;
                button3.BackColor = SystemColors.ButtonHighlight;
                button4.BackColor = SystemColors.ButtonHighlight;
                DateTime currentTime = DateTime.Now;
                string timeString = currentTime.ToString("HH:mm");
                textBox1.Text = timeString + "点击启动";
            }
            else
            {

                DateTime currentTime = DateTime.Now;
                string timeString = currentTime.ToString("HH:mm");
                textBox1.Text = timeString + "请先复位";
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = "停止中";
            zt = -1;
            pictureBox1.BackColor = Color.FromArgb(255, 255, 255, 128);
             den = "黄";
            button3.BackColor = Color.PaleTurquoise;
            button1.BackColor = SystemColors.ButtonHighlight;
            button2.BackColor = SystemColors.ButtonHighlight;
            button4.BackColor = SystemColors.ButtonHighlight;
            DateTime currentTime = DateTime.Now;
            string timeString = currentTime.ToString("HH:mm");
            textBox1.Text = timeString + "点击停止";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text = "待机中";
            zt = 0;
            pictureBox1.BackColor = Color.FromArgb(255, 255, 255, 128);
             den = "黄";
            button4.BackColor = Color.PaleTurquoise;
            button1.BackColor = SystemColors.ButtonHighlight;
            button2.BackColor = SystemColors.ButtonHighlight;
            button3.BackColor = SystemColors.ButtonHighlight;
            DateTime currentTime = DateTime.Now;
            string timeString = currentTime.ToString("HH:mm");
            textBox1.Text = timeString + "点击复位";
        }

       

    }
}
